﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [ScriptType("SP.FieldCollection", ServerTypeId="{d449d756-e113-4d27-a5e7-609cbc3eba7e}")]
    public class FieldCollection : ClientObjectCollection<Field>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public FieldCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public Field Add(Field field)
        {
            if (base.Context.ValidateOnClient && (field == null))
            {
                throw ClientUtility.CreateArgumentNullException("field");
            }
            Field field2 = new Field(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { field }));
            field2.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(field2.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, field2);
            base.Context.AddQuery(query);
            base.AddChild(field2);
            return field2;
        }

        [Remote]
        public Field AddDependentLookup(string displayName, Field primaryLookupField, string lookupField)
        {
            Field field = new Field(base.Context, new ObjectPathMethod(base.Context, base.Path, "AddDependentLookup", new object[] { displayName, primaryLookupField, lookupField }));
            field.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(field.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, field);
            base.Context.AddQuery(query);
            base.AddChild(field);
            return field;
        }

        [Remote]
        public Field AddFieldAsXml(string schemaXml, bool addToDefaultView, AddFieldOptions options)
        {
            if (base.Context.ValidateOnClient)
            {
                if (schemaXml == null)
                {
                    throw ClientUtility.CreateArgumentNullException("schemaXml");
                }
                if ((schemaXml != null) && (schemaXml.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("schemaXml");
                }
            }
            Field field = new Field(base.Context, new ObjectPathMethod(base.Context, base.Path, "AddFieldAsXml", new object[] { schemaXml, addToDefaultView, options }));
            field.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(field.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, field);
            base.Context.AddQuery(query);
            base.AddChild(field);
            return field;
        }

        [Remote]
        public Field GetById(Guid id)
        {
            Dictionary<Guid, Field> dictionary;
            object obj2;
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetById", out obj2))
            {
                dictionary = (Dictionary<Guid, Field>) obj2;
            }
            else
            {
                dictionary = new Dictionary<Guid, Field>();
                base.ObjectData.MethodReturnObjects["GetById"] = dictionary;
            }
            Field field = null;
            if (!dictionary.TryGetValue(id, out field))
            {
                field = new Field(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetById", new object[] { id }));
                dictionary[id] = field;
            }
            return field;
        }

        [Remote]
        public Field GetByInternalNameOrTitle(string strName)
        {
            Dictionary<string, Field> dictionary;
            object obj2;
            if (base.Context.ValidateOnClient)
            {
                if (strName == null)
                {
                    throw ClientUtility.CreateArgumentNullException("strName");
                }
                if ((strName != null) && (strName.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("strName");
                }
            }
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByInternalNameOrTitle", out obj2))
            {
                dictionary = (Dictionary<string, Field>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, Field>();
                base.ObjectData.MethodReturnObjects["GetByInternalNameOrTitle"] = dictionary;
            }
            Field field = null;
            if (!dictionary.TryGetValue(strName, out field))
            {
                field = new Field(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByInternalNameOrTitle", new object[] { strName }));
                dictionary[strName] = field;
            }
            return field;
        }

        [Remote]
        public Field GetByTitle(string displayName)
        {
            Dictionary<string, Field> dictionary;
            object obj2;
            if (base.Context.ValidateOnClient)
            {
                if (displayName == null)
                {
                    throw ClientUtility.CreateArgumentNullException("displayName");
                }
                if ((displayName != null) && (displayName.Length == 0))
                {
                    throw ClientUtility.CreateArgumentException("displayName");
                }
            }
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByTitle", out obj2))
            {
                dictionary = (Dictionary<string, Field>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, Field>();
                base.ObjectData.MethodReturnObjects["GetByTitle"] = dictionary;
            }
            Field field = null;
            if (!dictionary.TryGetValue(displayName, out field))
            {
                field = new Field(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByTitle", new object[] { displayName }));
                dictionary[displayName] = field;
            }
            return field;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            string str;
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (!flag && (((str = peekedName) != null) && (str == "SchemaXml")))
            {
                flag = true;
                reader.ReadName();
                base.ObjectData.Properties["SchemaXml"] = reader.ReadString();
            }
            return flag;
        }

        [Remote]
        public string SchemaXml
        {
            get
            {
                base.CheckUninitializedProperty("SchemaXml");
                return (string) base.ObjectData.Properties["SchemaXml"];
            }
        }
    }
}

